﻿using System;
using System.Collections.Generic;
using Extreme.SPFramework.BusinessLogicTemplate.Entities;
using Extreme.SPFramework.BusinessLogicTemplate.Interfaces;
using Extreme.SPFramework.DependencyInjection;
using Extreme.SPFramework.DomainModel;
using Extreme.SPFramework.Presentation;

namespace Extreme.SPFramework.BusinessLogicTemplate.PresentationModels
{
    /// <summary>
    /// Presentation model. Use with:
    ///     
    ///     public class MyControl : UserControlBase[MyPresentationModel]
    ///     {
    ///         ...you can use "base.PresentationModel.MyProperty" here...
    ///     }
    ///     
    /// </summary>
    [Serializable]
    public class MyPresentationModel : PresentationModelBase
    {
        private IMyService MyService
        {
            get { return ServiceProvider.GetInstance<IMyService>(); }
        }

        /// <summary>
        /// MyEntity Name property
        /// </summary>
        public string MyEntityName { get; set; }

        /// <summary>
        /// Owner login
        /// </summary>
        public string OwnerLogin { get; set; }

        /// <summary>
        /// Called by User Control
        /// </summary>
        public void Save(out List<string> errors)
        {
            errors = new List<string>();

            // TODO: validation
            MyService.Save(new MyEntity() 
            { 
                Name = MyEntityName, 
                Superior = new PersonInfo(OwnerLogin) 
            });
        }

        /// <summary>
        /// Gets all entities for some drop down list
        /// </summary>
        public List<MyEntity> GetAllEntities()
        {
            var allItems = MyService.GetAllItems();
            allItems.Insert(0, new MyEntity() { Name = "(Wszystkie)" });
            return allItems;
        }
    }
}
